package com.hsnn.medstgmini.base.dict.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.annotations.Source;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.base.dict.model.DicDrugspec;
import com.hsnn.medstgmini.base.dict.model.DicDrugspecLog;
import com.hsnn.medstgmini.base.dict.service.DicDrugspecLogManager;
import com.hsnn.medstgmini.base.dict.service.DicDrugspecManager;
import com.hsnn.medstgmini.util.Pagination;

/**
 * 基础库 数据字典 规格字典 控制层
 * @author hss@njhsnn
 * 2015年6月9日
 */
@Controller
@RequestMapping("/dicDrugspecLog")
public class DicDrugspecLogController {
	
	private static final Logger log = Logger.getLogger(DicDrugformLogController.class);
	
	@Autowired
	private HttpServletRequest request;

	@Source
	private DicDrugspecLogManager dicDrugspecLogManager;

	@Source
	private DicDrugspecManager dicDrugspecManager;

	@Autowired
	public void setDicDrugspecLogManager(DicDrugspecLogManager dicDrugspecLogManager) {
		this.dicDrugspecLogManager = dicDrugspecLogManager;
	}

	@Autowired
	public void setDicDrugspecManager(DicDrugspecManager dicDrugspecManager) {
		this.dicDrugspecManager = dicDrugspecManager;
	}

	/**
	 * 基础库 数据字典 规格字典 查询所有数据
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/log", method = {
			RequestMethod.GET, RequestMethod.POST })
	public String dicDrugspecLogDetail(HttpServletRequest request,HttpServletResponse response) throws IOException {
		String specId = request.getParameter("specId");
		String specName = new String(request.getParameter("specName").getBytes("ISO8859-1"),"utf-8");
		request.setAttribute("specName", specName);
		request.setAttribute("specId", specId);
		request.setAttribute("specCode", request.getParameter("specCode"));
		return "/base/dict/drugSpecHis";// 跳转到详情页面
	}

	/**
	 * 基础库 数据字典 规格字典 查询及条件查询
	 * 
	 * 用来封装json
	 * 
	 * @param request
	 * @param result
	 * @return
	 */
	@RequestMapping("/dicDrugspecLogList")
	@ResponseBody
	public Pagination getDicDrugspecLogList(HttpServletRequest request) {
		Pagination page = new Pagination(request);
		try {
			String date1 = request.getParameter("date1");
			String date2 = request.getParameter("date2");
			String specId = request.getParameter("specId");
			String specCode = request.getParameter("specCode");
			if(StringUtils.isNotBlank(date1)){
				page.getConditions().put("date1",date1.trim());
			}
			if(StringUtils.isNotBlank(date2)){
				page.getConditions().put("date2",date2.trim());
			}
			page.getConditions().put("specId",specId.trim());
			if(StringUtils.isNotBlank(specCode)){
				page.getConditions().put("specCode",specCode.trim());
			}
			Pagination data = dicDrugspecLogManager.queryDicDrugspecLogList(page);
			return data;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} catch (Error error) {
			error.printStackTrace();
			return null;
		}
	}
	
	
	/**
	 * 基础库 数据字典 剂型字典 根据id查询 规格 日志详情
	 * @param result
	 * @param dicDrugspecLog
	 * @return
	 */
	@RequestMapping("/toDrugSpecHisDetail")
	public String toDrugSpecHisDetail(){
		log.info("enter toDrugSpecHisDetail method!");
		try {
			Pagination  page = new Pagination();
			page.getConditions().put("logId", request.getParameter("logId"));
			List<Map<String,Object>> list = dicDrugspecLogManager.getDrugSpecHisDetail(page);
			List<Map<String,Object>> dataList = new ArrayList<Map<String,Object>>();
			List<Map<String,Object>> mapList = new ArrayList<Map<String,Object>>();
			String[] cColunm = {"一级规格编码","一级规格名称","二级规格编码","二级规格名称","状态"};
			String[] eColunm = {"fatherSpecCode","fatherSpecName","specCode","specName","status"};
			for(int i = 0;i<cColunm.length;i++){
				Map<String,Object> map =  new HashMap<String,Object>();
				map.put("colunm", cColunm[i]);//字段名
				map.put("after", !"status".equals(eColunm[i])?list.get(0).get(eColunm[i]):"1".equals(list.get(0).get(eColunm[i]).toString())?"启用":"停用");//修改后数据
				map.put("before", list.size()==1?null:!"status".equals(eColunm[i])?list.get(1).get(eColunm[i]):"1".equals(list.get(1).get(eColunm[i]).toString())?"启用":"停用");//修改前数据
				if(list.size()==1){
					map.put("red", "1");//是否变红加粗 1是 0 否
				}else{
					//判断修改后与修改前数据是否相等
					if(list.get(0).get(eColunm[i])!=null&&list.get(1).get(eColunm[i])!=null&&!list.get(0).get(eColunm[i]).toString().equals(list.get(1).get(eColunm[i]).toString())){
						map.put("red", "1");
					}else{
						map.put("red", "0");
					}
				}
				mapList.add(map);
			}
			for(int i = 0;i<mapList.size();i++){//把要变红的放在list前面
				if("1".equals(mapList.get(i).get("red"))){
					dataList.add(mapList.get(i));
				}
			}
			for(int i = 0;i<mapList.size();i++){
				if("0".equals(mapList.get(i).get("red"))){
					dataList.add(mapList.get(i));
				}
			}
			request.setAttribute("list", list);
			request.setAttribute("dataList", dataList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "/base/dict/drugSpecHisDetail";
		
	}
	
}
